package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author xunyuhua
 * @since 2022-07-06
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements IApBehaviorEntryService {

    @Override
    public ApBehaviorEntry getBehaviorEntryByUserIdOrEquipmentId(Integer userId, String equipmentId) {
        if (userId == 0) {//没有登录
            //根据设备号获取entry
            LambdaQueryWrapper<ApBehaviorEntry> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApBehaviorEntry::getEntryId, equipmentId);

            ApBehaviorEntry one = this.getOne(queryWrapper);
            if (one == null) {
                one = new ApBehaviorEntry();
                one.setEntryId(equipmentId);
                one.setType(0);
                one.setCreatedTime(new Date());
                this.save(one);
            }
            return one;
        } else {
            //根据设备号获取entry
            LambdaQueryWrapper<ApBehaviorEntry> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApBehaviorEntry::getEntryId, userId.toString());

            ApBehaviorEntry one = this.getOne(queryWrapper);
            if (one == null) {
                one = new ApBehaviorEntry();
                one.setEntryId(userId.toString());
                one.setType(1);
                one.setCreatedTime(new Date());
                this.save(one);
            }
            return one;

        }

    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(BehaviorDto dto) {
        Map map = new HashMap();
        map.put("isfollow", redisTemplate.hasKey("saveFollowBehavior_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_" + dto.getArticleId()));
        map.put("islike", redisTemplate.hasKey("saveLikeBehavior_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_" + dto.getArticleId()));
        map.put("isunlike", redisTemplate.hasKey("saveUnLikeBehavior_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_" + dto.getArticleId()));
        map.put("iscollection", redisTemplate.hasKey("saveCollectionBehavior_" + dto.getUserId() + "_" + dto.getEquipmentId() + "_" + dto.getArticleId()));
        return ResponseResult.okResult(map);

    }
}
